package com.team9.fitness.controller;

import com.team9.fitness.entity.ExerciseInfo;
import com.team9.fitness.service.ExerciseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运动信息控制器
 */
@RestController
@RequestMapping("/api/exercise-info")
@Slf4j
@CrossOrigin(origins = { "*" }, allowCredentials = "false")
public class ExerciseInfoController {

    @Autowired
    private ExerciseInfoService exerciseInfoService;

    /**
     * 创建运动信息
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createExerciseInfo(@RequestBody ExerciseInfo exerciseInfo) {
        Map<String, Object> response = new HashMap<>();

        try {
            ExerciseInfo createdExercise = exerciseInfoService.createExerciseInfo(exerciseInfo);
            response.put("success", true);
            response.put("message", "运动信息创建成功");
            response.put("data", createdExercise);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("创建运动信息失败", e);
            response.put("success", false);
            response.put("message", "创建失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取所有运动信息
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllExerciseInfo() {
        Map<String, Object> response = new HashMap<>();

        try {
            List<ExerciseInfo> exerciseList = exerciseInfoService.getAllExerciseInfo();
            response.put("success", true);
            response.put("data", exerciseList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取运动信息列表失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 根据ID获取运动信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getExerciseInfoById(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();

        try {
            ExerciseInfo exerciseInfo = exerciseInfoService.getExerciseInfoById(id);
            if (exerciseInfo != null) {
                response.put("success", true);
                response.put("data", exerciseInfo);
            } else {
                response.put("success", false);
                response.put("message", "运动信息不存在");
            }
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取运动信息失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 根据分类获取运动信息
     */
    @GetMapping("/category/{category}")
    public ResponseEntity<Map<String, Object>> getExerciseInfoByCategory(@PathVariable String category) {
        Map<String, Object> response = new HashMap<>();

        try {
            List<ExerciseInfo> exerciseList = exerciseInfoService.getExerciseInfoByCategory(category);
            response.put("success", true);
            response.put("data", exerciseList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取分类运动信息失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 搜索运动信息
     */
    @GetMapping("/search")
    public ResponseEntity<Map<String, Object>> searchExerciseInfo(@RequestParam String keyword) {
        Map<String, Object> response = new HashMap<>();

        try {
            List<ExerciseInfo> exerciseList = exerciseInfoService.searchExerciseInfoByName(keyword);
            response.put("success", true);
            response.put("data", exerciseList);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("搜索运动信息失败", e);
            response.put("success", false);
            response.put("message", "搜索失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取所有运动分类
     */
    @GetMapping("/categories")
    public ResponseEntity<Map<String, Object>> getAllCategories() {
        Map<String, Object> response = new HashMap<>();

        try {
            List<String> categories = exerciseInfoService.getAllCategories();
            response.put("success", true);
            response.put("data", categories);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取运动分类失败", e);
            response.put("success", false);
            response.put("message", "获取失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 更新运动信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateExerciseInfo(@PathVariable Long id,
            @RequestBody ExerciseInfo exerciseInfo) {
        Map<String, Object> response = new HashMap<>();

        try {
            exerciseInfo.setId(id);
            ExerciseInfo updatedExercise = exerciseInfoService.updateExerciseInfo(exerciseInfo);
            response.put("success", true);
            response.put("message", "运动信息更新成功");
            response.put("data", updatedExercise);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("更新运动信息失败", e);
            response.put("success", false);
            response.put("message", "更新失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 删除运动信息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteExerciseInfo(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();

        try {
            exerciseInfoService.deleteExerciseInfo(id);
            response.put("success", true);
            response.put("message", "运动信息删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("删除运动信息失败", e);
            response.put("success", false);
            response.put("message", "删除失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
}
